package com.ai.bitcoin.jpa.service.impl;

import com.ai.bitcoin.bo.BatchMonitorSettingBO;
import com.ai.bitcoin.bo.BatchMonitorSettingResultBO;
import com.ai.bitcoin.constant.CodeConstant;
import com.ai.bitcoin.constant.Errors;
import com.ai.bitcoin.enums.MonitorSwitchTypeEnum;
import com.ai.bitcoin.exception.BusinessException;
import com.ai.bitcoin.jpa.dao.BccBasicInfoDao;
import com.ai.bitcoin.jpa.dao.BccReportMonitorConfigDao;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import com.ai.bitcoin.jpa.entity.BccReportMonitorConfig;
import com.ai.bitcoin.jpa.service.BccReportMonitorConfigService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ai.bitcoin.constant.MonitorSettingConstrant.SWITCH_OFF;
import static com.ai.bitcoin.constant.MonitorSettingConstrant.SWITCH_ON;

/**
 * @description: ${description}
 * @author: gulj
 * @create: 2018-08-11 03:07
 **/

@Service
@Slf4j
public class BccReportMonitorConfigServiceImpl implements BccReportMonitorConfigService {

    @Autowired
    private BccReportMonitorConfigDao reportMonitorConfigDao;
    @Autowired
    private BccBasicInfoDao bccBasicInfoDao;


    @Override
    public List<BccReportMonitorConfig> getByOpenIdAndBccIdAndSwitchTypeEnum(String bccId, List<String> openIds, MonitorSwitchTypeEnum switchTypeEnum) {
        int status = 1;
        switch (switchTypeEnum) {
            case BOOM_BUST_SWITCH:
                return reportMonitorConfigDao.queryBoomBustSwitchExchangeAnnoSwitchByBccIdAndOpenIdsAndStatus(bccId, openIds, status);
            case ERC_TWO_ZERO_SWITCH:
                return reportMonitorConfigDao.queryErcTwoZeroSwitchExchangeAnnoSwitchByBccIdAndOpenIdsAndStatus(bccId, openIds, status);
            case NEWS_DYNAMIC_SWITCH:
                return reportMonitorConfigDao.queryNewsDynamicSwitchExchangeAnnoSwitchByBccIdAndOpenIdsAndStatus(bccId, openIds, status);
            case EXCHANGE_ANNO_SWITCH:
                return reportMonitorConfigDao.queryExchangeAnnoSwitchByBccIdAndOpenIdsAndStatus(bccId, openIds, status);
            case REPORT_SUBSCRIBE_SWITCH:
                return reportMonitorConfigDao.queryReportSubscribeSwitchExchangeAnnoSwitchByBccIdAndOpenIdsAndStatus(bccId, openIds, status);
            case BUYER_SELLER_POINT_SWITCH:
                return reportMonitorConfigDao.queryBuyerSellPointSwitchByBccIdAndOpenIdsAndStatus(bccId, openIds, status);
            default:
                return null;
        }
    }

    @Override
    @Transactional
    public void batchSetting(BatchMonitorSettingBO settingBO) throws BusinessException {
        List<String> bccIdList = bccBasicInfoDao.findAllBccId();
        if (StringUtils.isEmpty(settingBO.getOpenId())) {
            throw new BusinessException(Errors.PARAMS_ERROR, "非法请求。");
        }

        //补全用户bcc设置
        fillUserSetting(settingBO);
        //EC20配置
        reportMonitorConfigDao.updateErcTwoZeroByOpenId(settingBO.getOpenId(), bccIdList, SWITCH_OFF);
        if (CollectionUtils.isNotEmpty(settingBO.getErcTwoZeroList())) {
            reportMonitorConfigDao.updateErcTwoZeroByOpenId(settingBO.getOpenId(), settingBO.getErcTwoZeroList(), SWITCH_ON);
        }

        //暴涨配置
        reportMonitorConfigDao.updateBoomBustByOpenId(settingBO.getOpenId(), bccIdList, SWITCH_OFF);
        if (CollectionUtils.isNotEmpty(settingBO.getBoomBustList())) {
            reportMonitorConfigDao.updateBoomBustByOpenId(settingBO.getOpenId(), settingBO.getBoomBustList(), SWITCH_ON);
        }

        //交易所公告配置
        reportMonitorConfigDao.updateExchangeAnnoByOpenId(settingBO.getOpenId(), bccIdList, SWITCH_OFF);
        if (CollectionUtils.isNotEmpty(settingBO.getExchangeAnnoList())) {
            reportMonitorConfigDao.updateExchangeAnnoByOpenId(settingBO.getOpenId(), settingBO.getExchangeAnnoList(), SWITCH_ON);
        }

        //买点卖点配置
        reportMonitorConfigDao.updateBuyerSellerPointByOpenId(settingBO.getOpenId(), bccIdList, SWITCH_OFF);
        if (CollectionUtils.isNotEmpty(settingBO.getBuyerSellerPointList())) {
            reportMonitorConfigDao.updateBuyerSellerPointByOpenId(settingBO.getOpenId(), settingBO.getBuyerSellerPointList(), SWITCH_ON);
        }

        //报告订阅配置
        reportMonitorConfigDao.updateReportSubscribeByOpenId(settingBO.getOpenId(), bccIdList, SWITCH_OFF);
        if (CollectionUtils.isNotEmpty(settingBO.getReportSubscribeList())) {
            reportMonitorConfigDao.updateReportSubscribeByOpenId(settingBO.getOpenId(), settingBO.getReportSubscribeList(), SWITCH_ON);
        }

        //舆情动态配置
        reportMonitorConfigDao.updateNewsDynamicByOpenId(settingBO.getOpenId(), bccIdList, SWITCH_OFF);
        if (CollectionUtils.isNotEmpty(settingBO.getNewsDynamicList())) {
            reportMonitorConfigDao.updateNewsDynamicByOpenId(settingBO.getOpenId(), settingBO.getNewsDynamicList(), SWITCH_ON);
        }
    }

    /**
     * 取得用户监控配置
     *
     * @param openId 微信openId
     * @return
     */
    @Override
    public BatchMonitorSettingResultBO getMonitorBatchSetting(String openId) throws BusinessException {
        if (StringUtils.isEmpty(openId)) {
            throw new BusinessException(CodeConstant.PARAM_ERROR, "参数异常");
        }
        List<BccBasicInfo> bccList = bccBasicInfoDao.findAll();
        Map<String, BccBasicInfo> bccMap = Maps.newHashMap();
        for (BccBasicInfo bcc : bccList) {
            bccMap.put(bcc.getBccId(), bcc);
        }
        List<BccReportMonitorConfig> configList = reportMonitorConfigDao.findByOpenId(openId);
        BatchMonitorSettingResultBO setting = new BatchMonitorSettingResultBO();
        setting.setOpenId(openId);
        for (BccReportMonitorConfig config : configList) {
            BccBasicInfo bcc = bccMap.get(config.getBccId());
            Map<String, String> bccInfo = Maps.newHashMap();
            if (bcc != null) {
                bccInfo.put("bccId", config.getBccId());
                bccInfo.put("symbol", bcc.getSymbol());
            }
            else {
                bccInfo.put("bccId", config.getBccId());
                bccInfo.put("symbol", config.getBccNameEn());
            }

            //暴涨暴跌
            if (config.getBoomBustSwitch() == SWITCH_ON) {
                setting.getBoomBustList().add(bccInfo);
            }
            //erc20
            if (config.getErcTwoZeroSwitch() == SWITCH_ON) {
                setting.getErcTwoZeroList().add(bccInfo);
            }
            //交易所
            if (config.getExchangeAnnoSwitch() == SWITCH_ON) {
                setting.getExchangeAnnoList().add(bccInfo);
            }
            //买点卖点
            if (config.getBuyerSellerPointSwitch() == SWITCH_ON) {
                setting.getBuyerSellerPointList().add(bccInfo);
            }
            //订阅
            if (config.getReportSubscribeSwitch() == SWITCH_ON) {
                setting.getReportSubscribeList().add(bccInfo);
            }
            //舆情
            if (config.getNewsDynamicSwitch() == SWITCH_ON) {
                setting.getNewsDynamicList().add(bccInfo);
            }
        }
        return setting;
    }

    /**
     * 如果用户未配置相应的bccId 新增一条
     *
     * @param settingBO 配置
     */
    @SuppressWarnings("unchecked")
    private void fillUserSetting(BatchMonitorSettingBO settingBO) {
        //所有的BCCID 集合
        List<String> bccIdList = Lists.newLinkedList();
        bccIdList.addAll(settingBO.getBoomBustList());
        bccIdList.addAll(settingBO.getBuyerSellerPointList());
        bccIdList.addAll(settingBO.getErcTwoZeroList());
        bccIdList.addAll(settingBO.getExchangeAnnoList());
        bccIdList.addAll(settingBO.getNewsDynamicList());
        bccIdList.addAll(settingBO.getReportSubscribeList());

        List<String> unique = bccIdList.stream().distinct().collect(Collectors.toList());

        //查找有配置的bcc列表
        List<String> setting = reportMonitorConfigDao.findHasSetting(settingBO.getOpenId(), unique);

        //删除已设置
        unique.removeAll(setting);

        if (CollectionUtils.isEmpty(unique)) {
            return;
        }

        //增加未设置
        for (String bccId : unique) {
            // 未进行监控页面的任何设置，给用户初始化配置
            BccReportMonitorConfig build = new BccReportMonitorConfig();
            build.setBccId(bccId);
            BccBasicInfo info = bccBasicInfoDao.findByBccId(bccId);
            build.setOpenId(settingBO.getOpenId());
            build.setBccNameEn(info.getNameEn());
            build.setBoomBustSwitch(0);
            build.setNewsDynamicSwitch(0);
            build.setBuyerSellerPointSwitch(0);
            build.setErcTwoZeroSwitch(0);
            build.setReportSubscribeSwitch(0);
            build.setExchangeAnnoSwitch(0);
            build.setCreateTime(new Date());
            build.setUpdateTime(new Date());
            build.setCoinPriceRate(1);
            reportMonitorConfigDao.save(build);
            log.info("用户{}未进行 bccId:{}监控页面的任何设置。系统自动补充一条默认配置。", settingBO.getOpenId(), bccId);
        }
    }
}
